<?php
// +----------------------------------------------------------------------------
// | Tke团队版权所属 [三十年河东三十年河西,莫欺少年穷.!]
// +----------------------------------------------------------------------------
// | Copyright (c) 2014 http://www.itdaodan.com All rights reserved.
// +----------------------------------------------------------------------------
// | Author: sap<songaiping@hrbjjhh.com>
// +----------------------------------------------------------------------------

namespace Admin\Model;
use Admin\Model\BaseModel;
/**
 * @todo 质检店铺模型
 */

class CheckShopModel extends BaseModel {

    /* 自动验证规则 */
    protected $_validate = array(
        array('activity_id', 'isActivity', '必须选择活动', self::MUST_VALIDATE, 'function', self::MODEL_BOTH),
        array('merchant_id', 'isMerchant', '必须选择商户', self::MUST_VALIDATE, 'function', self::MODEL_BOTH),
        array('shop_id', 'isShop', '必须选择店铺', self::MUST_VALIDATE, 'function', self::MODEL_BOTH),
        array('is_pass', '0,1', '质检店铺状态不正确', self::MUST_VALIDATE, 'in', self::MODEL_BOTH),
    	array('fail_order', 'number', '排序必须为数字', self::VALUE_VALIDATE, 'regex', self::MODEL_BOTH),
        array('remark', 'require', '请填写备注信息', self::VALUE_VALIDATE, 'regex', self::MODEL_BOTH),
        array('create_time', 'require', '质检时间不能为空', self::MUST_VALIDATE, 'regex', self::MODEL_BOTH),
        array('apply_start', 'require', '开始申请核销时间不能为空', self::MUST_VALIDATE, 'regex', self::MODEL_BOTH),
        array('apply_end', 'require', '结束申请核销时间不能为空', self::MUST_VALIDATE, 'regex', self::MODEL_BOTH),
    );

    /* 自动完成规则 */
    protected $_auto = array(
        array('create_time', 'strtotime', self::MODEL_BOTH, 'function'),
        array('apply_start', 'strtotime', self::MODEL_BOTH, 'function'),
        array('apply_end', 'strtotime', self::MODEL_BOTH, 'function'),
    	array('status', '1', self::MODEL_INSERT),
        array('member_id', UID, self::MODEL_BOTH)
    );

    /**
     * @todo 新增或更新一个质检店铺
     * @return boolean fasle 失败 ， int  成功 返回ID
     * @author sap
     * @since 2016-3-21
     */
    public function update(){
        /* 获取数据对象 */
    	$data = $this->create($_POST);
        if(empty($data)){
            return false;
        }
        
        /* 添加或更新数据 */
        if(empty($data['check_shop_id'])){ //新增数据
            $check_shop_id = $this->add();
            if(!$check_shop_id){
                $this->error = '新增质检店铺出错！';
                return false;
            }
            action_log('check_shop_add', 'check_shop', $check_shop_id, UID);
        } else { //更新数据
            $status = $this->save(); //更新基础内容
            if(false === $status){
                $this->error = '更新质检店铺出错！';
                return false;
            }else{
            	$check_shop_id = $data['check_shop_id'];
            	action_log('check_shop_edit', 'check_shop', $check_shop_id, UID);
            }
        }
        //内容添加或更新完成
        return $check_shop_id;
    }

    /**
     * @todo 质检店铺详情
     * @return false|array
     * @author sap
     */
    public function checkShopShow($check_shop_id=0,$field=''){
    	//获取数据
    	$check_shop_id = (int)$check_shop_id;
    	if($check_shop_id == 0){
    		$this->error = '请正确操作';
    		return false;
    	}
    	$map = array();//初始化查询条件
    	//查询条件
    	$map['check_shop_id'] = $check_shop_id;
    	$map['status'] = array('gt',-1);
    	//返回值
    	return $this->where($map)->field($field)->find();
    }
    
    /**
     * @todo 回访统计
     * @author sap
     * @since 2016-3-22
     */ 
    public function checkStatistics(){
        $activity_id = (int)I('activity_id');//活动搜索ID号
        $merchant_id = (int)I('merchant_id');//商户搜索ID号
        $shop_id = (int)I('shop_id');//店铺搜索ID号
        $order_status = (int)I('order_status',100);//订单状态
        $apply_start_time = (I('apply_start_time'))? strtotime(I('apply_start_time')) : 1 ;//申请核销开始时间
        $apply_end_time = (I('apply_end_time'))? strtotime(I('apply_end_time')) : NOW_TIME ;//申请核销结束时间
        //基础判断及页面搜索信息列表
        if($activity_id == 0 || $merchant_id == 0 || $shop_id == 0){
            $this->error = '请选择店铺信息';
            return false;
        }
        if($apply_start_time > $apply_end_time){
            $this->error = '开始时间不能大于结束时间';
            return false;
        }
        //返回数组初始化
        $info = array();
        $info['is_complain_num'] = 0;//申诉数量
        $info['is_code_num'] = 0;//申请识别码数量
        $info['order_num'] = 0;//申请核销数量
        $info['check_order_num'] = 0;//回访数量
        $info['activity_name'] = getActivityName($activity_id);//活动名称
        $visit_type = C('VISIT_TYPE');
        foreach ($visit_type as $k=>$v){
            $info['visit_type_'.$k] = 0;//各种状态下回访数量
        }
        //质检店铺信息
        $map_shop['activity_id'] = $activity_id;
        $map_shop['merchant_id'] = $merchant_id;
        $map_shop['shop_id'] = $shop_id;
        $map_shop['status'] = array('gt',-1);
        $map_shop['is_pass'] = 0;
        $is_pass_num = $this->where($map_shop)->count();//不合格次数
        $info['shop_name'] = getShopName($shop_id);//店铺名称
        $info['is_pass_num'] = $is_pass_num;//店铺不合格次数
        //搜索order表
        $map = array();
        $map['shop_id'] = $shop_id;
        $map['activity_id'] = $activity_id;
        if($order_status != 100){
            $map['order_status'] = $order_status;
        }
        $map['apply_time'] = array('between', array($apply_start_time, $apply_end_time));
        $order_list = M('Order')->where($map)->field('order_id')->select();
        if(empty($order_list)){
            return $info;
        }
        $order_id = array_column($order_list, 'order_id');
        $info['order_num'] = count($order_id);//申请核销数量
        
        //搜索CheckOrder表
        $obj = M('CheckOrder');
        $where = array();
        $where['order_id'] = array('in',$order_id);
        $check_order_info = $obj->where($where)->field('visit_type,count(*) as total')->group('visit_type')->select();
        if(empty($check_order_info)){
            return $info;
        }
        foreach ($check_order_info as $key=>$val){
            if(isset($visit_type[$val['visit_type']])){
                $info['visit_type_'.$val['visit_type']] = $val['total'];//各种状态下回访数量
                $info['check_order_num'] += $val['total'];//回访数量
            }
        }
        $where['is_complain'] = 1;//1是申诉，0没有申诉
        $info['is_complain_num'] = $obj->where($where)->count();
        unset ($where['is_complain']);//释放给定的变量
        $where['is_code'] = 1;//1是申请识别码，0没有申请识别码
        $info['is_code_num'] = $obj->where($where)->count();
        return $info;
    }
    
}
